package com.landleaf.smarthome.net.mqtt;

import com.landleaf.smarthome.App;
import com.landleaf.smarthome.db.dao.DeviceDaoHelper;
import com.landleaf.smarthome.model.DeviceAttribute;
import com.landleaf.smarthome.model.DeviceModel;
import com.landleaf.smarthome.net.hardware.HardwareExecutor;
import com.landleaf.smarthome.net.http.ApiFactory;
import com.landleaf.smarthome.net.mqtt.bean.MqttMsgBodyModel;

import java.util.List;

/**
 * When I wrote this, only God and I understood what I was doing
 * Now, God only knows
 * Created by 海航
 * 2020/9/27
 * 功能描述: mqtt收到的消息处理类
 */
public class MqttMsgHandler {
    /**
     * app远程控制设备
     *
     * @param data
     */
    public void handlerDeviceWriteMsg(MqttMsgBodyModel data) {
        MqttExecutor.getInstance().sendCommonReturnMsg(data);
        // TODO: 2020/10/12  通过data里面的设备数据控制设备信息
        int productCode = data.getPayload().getData().getProductCode();
        String deviceSn = data.getPayload().getData().getDeviceSn();
        List<DeviceAttribute> atts = data.getPayload().getData().getItems();
        //通过sn号找到设备信息
        DeviceDaoHelper deviceDaoHelper = new DeviceDaoHelper();
        DeviceModel deviceModel = deviceDaoHelper.findDevice4Sn(deviceSn);
        if (atts != null && atts.size() > 0) {
            HardwareExecutor.getInstance(App.getInstance()).deviceWriteAsyn(deviceModel, atts.get(0));
        }
    }



    /**
     * app读取设备信息
     *
     * @param data
     */
    public void handlerDeviceReadMsg(MqttMsgBodyModel data) {
        // TODO: 2020/10/12  通过data里面的设备数据读取设备信息
        int productCode = data.getPayload().getData().getProductCode();
        String deviceSn = data.getPayload().getData().getDeviceSn();
        //通过sn号找到设备信息
        DeviceDaoHelper deviceDaoHelper = new DeviceDaoHelper();
        DeviceModel deviceModel = deviceDaoHelper.findDevice4Sn(deviceSn);
        HardwareExecutor.getInstance(App.getInstance()).deviceReadAsyn(deviceModel);
    }

    /**
     * app远程设置场景状态
     *
     * @param data
     */
    public void handlerSceneSetMsg(MqttMsgBodyModel data) {
        MqttExecutor.getInstance().sendCommonReturnMsg(data);
        // TODO: 2020/10/12  控制场景
        String sceneId = data.getPayload().getData().getSceneId();

    }

    /**
     * 部分配置信息发生变化
     *
     * @param data
     */
    public void handlerConfigUpdateMsg(MqttMsgBodyModel data) {
        MqttExecutor.getInstance().sendCommonReturnMsg(data);
        String type = data.getPayload().getData().getUpdateType();
        if (type!=null){
            switch (type){
                case MqttConst.CONFIG_APK_UPDATE:
                   //请求更新apk的接口
                 //   ApiFactory.getNetApi().apkUpdate()
                    break;
                case MqttConst.CONFIG_SCENE:
                    //请求获取场景的接口
                    ApiFactory.getNetApi().getSceneList().subscribe();
                    break;
                case MqttConst.CONFIG_TIMING_SCENE:
                    //请求获取定时场景的接口
                    ApiFactory.getNetApi().getTimingSceneList().subscribe();
                    break;
                case MqttConst.CONFIG_FLOOR_ROOM_DEVICE:
                    //请求获取房间设备信息的接口
                    ApiFactory.getNetApi().getFloorDeviceList().subscribe();
                    break;
                case MqttConst.CONFIG_NEWS:
                    //请求获取新闻信息的接口
                    ApiFactory.getNetApi().getNewsList().subscribe();
                    break;
            }
        }
    }

    public void handlerDeviceUpdateMsg(MqttMsgBodyModel data) {
        // TODO: 2020/10/12  这个目前只有大屏对云端  所以暂时不用做处理

    }

    /**
     * 大屏上报云端 暂时不用做处理
     *
     * @param data
     */
    public void handlerSecurityAlarmMsg(MqttMsgBodyModel data) {
        // TODO: 2020/10/12  这个目前只有大屏对云端  所以暂时不用做处理
    }
}
